package com.blade.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;

/**
 * 支持中文的Base64编码解码类
 */
public class Base64Coder {
    public Base64Coder() {
    }

    public static byte[] encryptBase64(byte[] data) {
        return Base64.encodeBase64(data);
    }

    public static byte[] encryptBase64(String data) {
        return Base64.encodeBase64(getBytesUtf8(data));
    }

    public static String encryptBase64String(byte[] data) {
        byte[] bytes = Base64.encodeBase64(data);
        return newStringUtf8(bytes);
    }

    public static String encryptBase64String(String data) {
        byte[] bytes = Base64.encodeBase64(getBytesUtf8(data));
        return newStringUtf8(bytes);
    }

    public static byte[] decryptBase64(byte[] data) {
        return Base64.decodeBase64(data);
    }

    public static byte[] decryptBase64(String data) {
        return Base64.decodeBase64(data);
    }

    public static byte[] getBytesUtf8(String string) {
        return StringUtils.getBytesUtf8(string);
    }

    public static String newStringUtf8(byte[] bytes) {
        return StringUtils.newStringUtf8(bytes);
    }

    public static void main(String[] strings) {
        String goods = "水果";

        String ecode = Base64Coder.encryptBase64String(goods);

        System.out.println(ecode);

        System.out.println(Base64Coder.decryptBase64(ecode));
    }

}
